﻿//-----------------------------------------------------------------------
// <copyright file="OptionGroupWithDropDownList.aspx.cs" company="CMSN Software">
//    Copyright © 2011  CMSN Software
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see http://www.gnu.org/licenses.
// </copyright>
//-----------------------------------------------------------------------
namespace HowToUseOptionGroupWithDropDownList
{
    using System;
    using System.Data;
    using System.Globalization;

    /// <summary>
    /// usage of custome dropdown list
    /// </summary>
    public partial class OptionGroupWithDropDownList : System.Web.UI.Page
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.DropDownWithOptionGroup.DataSource = SampleData().Tables[0].DefaultView;
            this.DropDownWithOptionGroup.DataTextField = "FirstColumn";
            this.DropDownWithOptionGroup.DataValueField = "SecondColumn";
            this.DropDownWithOptionGroup.DataOptionGroupField = "ThirdColumn";
            this.DropDownWithOptionGroup.DataBind();
        }

        /// <summary>
        /// Sample data.
        /// </summary>
        /// <returns>data set with sample data</returns>
        private static DataSet SampleData()
        {
            DataSet sampleDataSet = new DataSet();
            sampleDataSet.Locale = CultureInfo.InvariantCulture;
            DataTable sampleDataTable = sampleDataSet.Tables.Add("SampleData");

            sampleDataTable.Columns.Add("FirstColumn", typeof(string));
            sampleDataTable.Columns.Add("SecondColumn", typeof(string));
            sampleDataTable.Columns.Add("ThirdColumn", typeof(string));
            DataRow sampleDataRow;
            for (int i = 1; i <= 49; i++)
            {
                sampleDataRow = sampleDataTable.NewRow();
                sampleDataRow["FirstColumn"] = "Cell1: " + i;
                sampleDataRow["SecondColumn"] = "Cell2: " + i;
                sampleDataRow["ThirdColumn"] = "Cell3: " + i.ToString(CultureInfo.InvariantCulture).Substring(0, 1);
                sampleDataTable.Rows.Add(sampleDataRow);
            }

            return sampleDataSet;
        }
    }
}